package cn.t.repository.impl.user;

import cn.t.converter.user.RoleInfoConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.RoleInfoDO;
import cn.t.persistence.entity.user.RoleInfoPO;
import cn.t.persistence.mapper.user.RoleInfoMapper;
import cn.t.repository.user.RoleInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表数据仓库
 *
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RoleInfoRepositoryImpl extends RepositoryImpl<RoleInfoMapper, RoleInfoPO, RoleInfoConverter, RoleInfoDO> implements RoleInfoRepository {


    @Override
    public List<RoleInfoDO> findByModel(RoleInfoDO roleInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<RoleInfoPO> list = getWrapper(roleInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<RoleInfoPO> getWrapper(RoleInfoDO roleInfoDO) {
        LambdaQueryChainWrapper<RoleInfoPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(roleInfoDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(roleInfoDO.getGmtCreatedEnd())) {
            wrapper.between(RoleInfoPO::getGmtCreated, roleInfoDO.getGmtCreatedStart(), roleInfoDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(roleInfoDO.getGmtModifiedEnd())) {
            wrapper.between(RoleInfoPO::getGmtModified, roleInfoDO.getGmtModifiedStart(), roleInfoDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getId())) {
            wrapper.eq(RoleInfoPO::getId, roleInfoDO.getId());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getParticipantId())) {
            wrapper.eq(RoleInfoPO::getParticipantId, roleInfoDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getRoleName())) {
            wrapper.eq(RoleInfoPO::getRoleName, roleInfoDO.getRoleName());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getRoleCode())) {
            wrapper.eq(RoleInfoPO::getRoleCode, roleInfoDO.getRoleCode());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getRoleType())) {
            wrapper.eq(RoleInfoPO::getRoleType, roleInfoDO.getRoleType());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getCounter())) {
            wrapper.eq(RoleInfoPO::getCounter, roleInfoDO.getCounter());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getRemark())) {
            wrapper.eq(RoleInfoPO::getRemark, roleInfoDO.getRemark());
        }
        if (!ObjectUtils.isEmpty(roleInfoDO.getIsDefault())) {
            wrapper.eq(RoleInfoPO::getIsDefault, roleInfoDO.getIsDefault());
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(roleInfoDO.getEmailAddress())) {
            wrapper.eq(RoleInfoPO::getEmailAddress, roleInfoDO.getEmailAddress());
        }

        if (!ObjectUtils.isEmpty(roleInfoDO.getIsDeleted())) {
            wrapper.eq(RoleInfoPO::getIsDeleted, roleInfoDO.getIsDeleted());
        } else {
            wrapper.eq(RoleInfoPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        if (!ObjectUtils.isEmpty(roleInfoDO.getStatus())) {
            wrapper.eq(RoleInfoPO::getStatus, roleInfoDO.getStatus());
        } else {
            wrapper.eq(RoleInfoPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }
        wrapper.in(!CollectionUtils.isEmpty(roleInfoDO.getRoleIds()), RoleInfoPO::getId, roleInfoDO.getRoleIds());
        return wrapper;
    }

    @Override
    public long count(RoleInfoDO roleInfoDO) {
        return getWrapper(roleInfoDO).count();
    }


    @Override
    public boolean deleteRoleById(Long roleId) {
//        return getBaseMapper().deleteRoleById(roleId);
        return lambdaUpdate().set(RoleInfoPO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(RoleInfoPO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(RoleInfoPO::getGmtModified, LocalDateTime.now()).eq(RoleInfoPO::getId, roleId).update();
    }

    @Override
    public RoleInfoDO findByFirmAndId(Long firmId, Long roleId) {

        List<RoleInfoPO> list = getBaseMapper().findByFirmAndId(firmId, roleId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return converter.po2Model(list.get(0));
    }

    @Override
    public List<RoleInfoDO> getRoleInfoByNameAndFirm(String roleName, Long firmId) {

        List<RoleInfoPO> list = getBaseMapper().getRoleInfoByNameAndFirm(roleName, firmId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }
}
